﻿using System;
using System.Text;

namespace DflyingForum.Core.Framework
{
    /// <summary>
    /// 分页
    /// </summary>
    public static class HtmlPager
    {
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="currentPage">当前页</param>
        /// <param name="recordCount">记录总数</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="url">原url</param>
        /// <param name="queryNames">url查询参数名</param>
        /// <param name="queryValues">url查询参数值</param>
        /// <returns></returns>
        public static string GetPager(int currentPage, int recordCount, int pageSize, string url, string[] queryNames, string[] queryValues)
        {
            //总页数
            int totalPage = recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1;

            //初始化url参数
            string urlQuery = string.Empty;
            for (int i = 0; i < queryNames.Length; i++)
            {
                urlQuery += "&" + queryNames[i] + "=" + queryValues[i];
            }
            url += url.IndexOf('?') > -1 ? "&" : "?" + "page={0}" + urlQuery;

            //写html分页布局
            int step = 10;
            int pageRoot = 1;//页根
            if (currentPage - step / 2 < 2)//当前页左边显示5个
                pageRoot = 1;
            else
                pageRoot = currentPage - 5;

            int pageFoot = totalPage;//页脚
            if (currentPage - (step - 2) / 2 <= 2)//当前页右边显示4个
                pageFoot = totalPage >= step ? step : totalPage;
            else
                pageFoot = currentPage + (step - 2) / 2 >= totalPage ? totalPage : currentPage + (step - 2) / 2;

            StringBuilder sb = new StringBuilder();
            if (pageRoot > 1)//首页
                sb.Append(string.Format("<a href='{0}'>1...</a>\r", string.Format(url, 1)));
            if (currentPage > pageRoot)//上页
                sb.Append(string.Format("<a href='{0}'>上一页</a>\r", string.Format(url, currentPage - 1)));

            //只输出页头到页脚
            for (int i = pageRoot; i <= pageFoot; i++)
            {
                if (i == currentPage)
                {
                    sb.Append(string.Format("<strong><a href='javascript:;' style='background-color:#e6e6e6; border:1px solid #ababab;'>{0}</a></strong>\r", i));//当前页
                }
                else
                {
                    sb.Append(string.Format("<a href='{0}'>{1}</a>\r", string.Format(url, i), i));
                }
                if (i == totalPage)
                    break;
            }

            if (pageFoot < totalPage)//末页
                sb.Append(string.Format("<a href='{0}'>...{1}</a>\r", string.Format(url, totalPage), totalPage));
            if (currentPage < pageFoot)//下页
                sb.Append(string.Format("<a href='{0}'>下一页</a>\r", string.Format(url, currentPage + 1)));

            return sb.ToString();
        }

        /// <summary>
        /// 主要用于Ajax分页  
        /// jsFunction js函数名
        /// values函数参数
        /// </summary>
        public static string GetPager(int currentPage, int recordCount, int pageSize, string jsFunction, string[] values)
        {
            //总页数
            int totalPage = recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1;

            //初始化js函数参数
            jsFunction += "(";
            for (int i = 0; i < values.Length; i++)
            {
                jsFunction += values[i] + ",";
            }
            jsFunction += "{0}";//最后一个是page参数
            jsFunction += ")";

            //写html分页布局
            int step = 10;
            int pageRoot = 1;//页根
            if (currentPage - step / 2 < 2)//当前页左边显示5个
                pageRoot = 1;
            else
                pageRoot = currentPage - 5;

            int pageFoot = totalPage;//页脚
            if (currentPage - (step - 2) / 2 <= 2)//当前页右边显示4个
                pageFoot = totalPage >= step ? step : totalPage;
            else
                pageFoot = currentPage + (step - 2) / 2 >= totalPage ? totalPage : currentPage + (step - 2) / 2;

            StringBuilder sb = new StringBuilder();
            if (pageRoot > 1)//首页
                sb.Append(string.Format("<a href='javascript:;' onclick='{0}'>1...</a>\r", string.Format(jsFunction, 1)));
            if (currentPage > pageRoot)//上页
                sb.Append(string.Format("<a href='javascript:;' onclick='{0}'>上一页</a>\r", string.Format(jsFunction, currentPage - 1)));

            //只输出页头到页脚
            for (int i = pageRoot; i <= pageFoot; i++)
            {
                if (i == currentPage)
                {
                    sb.Append(string.Format("<strong><a href='javascript:;' style='background-color:#e6e6e6; border:1px solid #ababab;'>{0}</a></strong>\r", i));//当前页
                }
                else
                {
                    sb.Append(string.Format("<a href='javascript:;' onclick='{0}'>{1}</a>\r", string.Format(jsFunction, i), i));
                }
                if (i == totalPage)
                    break;
            }

            if (pageFoot < totalPage)//末页
                sb.Append(string.Format("<a href='javascript:;' onclick='{0}'>...{1}</a>\r", string.Format(jsFunction, totalPage), totalPage));
            if (currentPage < pageFoot)//下页
                sb.Append(string.Format("<a href='javascript:;' onclick='{0}'>下一页</a>\r", string.Format(jsFunction, currentPage + 1)));

            return sb.ToString();
        }
    }
}
